load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
load("//rs/tests:common.bzl", "MAINNET_ENV", "MESSAGE_CANISTER_ENV", "MESSAGE_CANISTER_RUNTIME_DEPS", "UNIVERSAL_CANISTER_ENV", "UNIVERSAL_CANISTER_RUNTIME_DEPS", "UNIVERSAL_VM_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "system_test_nns")

package(default_visibility = ["//rs:system-tests-pkg"])

rust_library(
    name = "common",
    testonly = True,
    srcs = ["common.rs"],
    crate_name = "ic_consensus_system_test_backup_common",
    deps = [
        # Keep sorted.
        "//rs/backup",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:hex",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:tempfile",
    ],
)

BACKUP_RUNTIME_DEPS = MESSAGE_CANISTER_RUNTIME_DEPS + UNIVERSAL_CANISTER_RUNTIME_DEPS + [
    # Keep sorted.
    "//rs/backup:ic-backup",
    "//rs/canister_sandbox:canister_sandbox",
    "//rs/canister_sandbox:compiler_sandbox",
    "//rs/canister_sandbox:sandbox_launcher",
    "//rs/replay:ic-replay",
]

BACKUP_ENV = MESSAGE_CANISTER_ENV | UNIVERSAL_CANISTER_ENV | {
    "IC_BACKUP_PATH": "$(rootpath //rs/backup:ic-backup)",
    "IC_REPLAY_PATH": "$(rootpath //rs/replay:ic-replay)",
    "COMPILER_SANDBOX_PATH": "$(rootpath //rs/canister_sandbox:compiler_sandbox)",
    "SANDBOX_LAUNCHER_PATH": "$(rootpath //rs/canister_sandbox:sandbox_launcher)",
    "CANISTER_SANDBOX_PATH": "$(rootpath //rs/canister_sandbox:canister_sandbox)",
}

rust_binary(
    name = "backup_manager_test_bin",
    testonly = True,
    srcs = ["backup_manager_test.rs"],
    crate_name = "ic_systest_backup",
    deps = [
        # Keep sorted.
        ":common",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "backup_manager_upgrade_test",
    env = BACKUP_ENV | MAINNET_ENV | {
        "ENV_DEPS__IC_VERSION_FILE": "$(rootpath //bazel:version.txt)",
    },
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
    ],
    test_driver_target = ":backup_manager_test_bin",
    test_timeout = "eternal",  # this test often times out with the default 15 minute timeout so we allow more time
    uses_guestos_img = False,
    uses_guestos_mainnet_nns_img = True,
    uses_guestos_update = True,
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        BACKUP_RUNTIME_DEPS + ["//bazel:version.txt"],
)

system_test_nns(
    name = "backup_manager_downgrade_test",
    env = BACKUP_ENV | MAINNET_ENV | {
        "ENV_DEPS__IC_VERSION_FILE": "$(rootpath //bazel:version.txt)",
    },
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
    ],
    test_driver_target = ":backup_manager_test_bin",
    test_timeout = "eternal",  # this test often times out with the default 15 minute timeout so we allow more time
    uses_guestos_img = True,
    uses_guestos_mainnet_nns_update = True,
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        BACKUP_RUNTIME_DEPS + ["//bazel:version.txt"],
)
